<?php

class EmploymentContractDao extends BaseDao
{

    public function getEmploymentContractListByEmployeeNumber($empNumber, $sortField = 'start_date', $sortOrder = 'ASC', $limit = null, $offset = null)
    {

        $sortField = ($sortField == "") ? 'start_date' : $sortField;
        $sortOrder = ($sortOrder == "") ? 'ASC' : $sortOrder;

        try {
            $q = Doctrine_Query :: create()
                ->from('EmploymentContract')
                ->where('emp_number = ?', $empNumber);

            $q->orderBy($sortField . ' ' . $sortOrder);
            if (!empty($limit)) {
                $q->offset($offset)
                    ->limit($limit);
            }

//            print $q->getSqlQuery();
//            print '<pre>';
//            print_r ($q->getParams());
//            var_dump($q->execute());
//            print '</pre>';

            return $q->execute();
        } catch (Exception $e) {
            throw new DaoException($e->getMessage());
        }
    }

    public function deleteEmploymentContract($toBeDeletedEmploymentContractIds)
    {

        try {
//            $q = Doctrine_Query :: create()
//                ->update('EmploymentContract')
//                ->set('isDeleted', '?', EmploymentContract::DELETED)
//                ->whereIn('id', $toBeDeletedEmploymentContractIds);

            $q = Doctrine_Query :: create()->delete('EmploymentContract')
                ->whereIn('id', $toBeDeletedEmploymentContractIds);

            return $q->execute();
        } catch (Exception $e) {
            throw new DaoException($e->getMessage());
        }
    }

    public function getEmploymentContractById($employmentContractId)
    {

        try {
            return Doctrine::getTable('EmploymentContract')->find($employmentContractId);
        } catch (Exception $e) {
            throw new DaoException($e->getMessage());
        }
    }

}

